<?php
//系统维护类
class systemMod extends commonMod {  
	//用户管理
	public function accountManagement(){
		 require(CP_PATH.'ext/Page.class.php');
		 //获取页数ID
         $page_id=intval($_GET['page']); 
         $name = $_GET['name'];
         if($name){
         	$condition = "username like '%".$name."%'";  
         	$con=" where username like '%".$name."%' ";
         }
         $url=__URL__.'/page?page='.$page_id;
         $page=new Page();
         //每页显示行数
         $listRows=10;//每页显示的信息条数
         //每页显示页数     
         $pageBarNum=5;
         $cur_page=$page->getCurPage($url);  
         
   		 $limit_start=($cur_page-1)*$listRows;   
       	 $limit=$limit_start.','.$listRows; 
		 $limitStr='';
       	 if($limit!=''){
       	 	$limitStr = " limit ".$limit;
       	 }
         
  	 	$count=$this->model->table('h_admin')->where($condition)->count(); 
  		
  		$sql ="select p.rolename,a.* from h_permission p left join h_admin a on a.level=p.id order by a.id".$con.$limitStr;
		$list  =   $this->model->query($sql);
		 
  		$this->assign('count',$count);
  		if($listRows>=$count){
  			 $this->assign('page','');
  		}else{
    	   $this->assign('page',$page-> show($url,$count,$listRows,$pageBarNum,$mode=5) );    
  		}
		if(count($list)){
			$this->assign("adminArr",$list);	
		}
		
		/*$sqlj = "select p.* from h_permission p left join h_admin a on a.level=p.id order by a.id";
		$listj= $this->model->query($sqlj);		
		if ($listj){
			$this->assign("rolenameArr",$listj);	
		}
		*/
		$sqlm = "select * from h_permission ";
		$listm = $this->model->query($sqlm);		
		if ($listm){
			$this->assign("permissionArr",$listm);	
		}


		$this->display("system/accountManagement");
	}
	
	//用户添加
	public function addadmin(){
		$json ='{';
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$uname =  $_POST['uname'];
		$level =  $_POST['level'];
        $department =  $_POST['department'];
        $post =  $_POST['post'];
        $phone =  $_POST['phone'];
        $landline =  $_POST['landline'];
        $email =  $_POST['email'];
        $other =  $_POST['other'];

        $id =  $_POST['id'];
        if($id){
            $sql="SELECT * FROM `h_admin` WHERE id!=".$id." and username='".$username."'";
            $row1 = $this->model->query($sql);
            if($row1){
                $this->error('用户已经存在');
            }
            $sql = "update h_admin set username='".$username."',password='".$password."',uname='".$uname."',`department`='".$department."',`post`='".$post."',`phone`='".$phone."',`landline`='".$landline."',`email`='".$email."',`other`='".$other."',level='".$level."' where id=".$id;
            $row = $this->model->query($sql);
            $_SESSION['manage_username']=$username;
            $_SESSION['manage_uname']=$uname;

            $info = $this->model->table('h_admin')->where("username='".$username."'")->find();
            $_SESSION['manage_department']=$info['department'];
            $_SESSION['manage_post']=$info['post'];
            $_SESSION['manage_phone']=$info['phone'];
            $_SESSION['manage_landline']=$info['landline'];
            $_SESSION['manage_email']=$info['email'];
            $_SESSION['manage_other']=$info['other'];
            $this->addStaffLog('修改用户','修改用户：'.$username,'user');
        }else{
            $al = $this->search_already('username',$username,'h_admin');//判断用户是否存在
            if($al==1){
                $this->error('用户已经存在');
            }
            $sql = "insert into h_admin( `username`, `password`, `uname`, `department`, `post`, `phone`, `landline`, `email`, `other`,  `level`) values".
                "('{$username}','{$password}','{$uname}','{$department}','{$post}','{$phone}','{$landline}','{$email}','{$other}','{$level}')";

            $row = $this->model->query($sql);
            $this->addStaffLog('添加用户','添加用户：'.$username,'user');
        }
        $this->redirect(__APP__."/system/accountManagement");
	}
	

	//用户删除
	public function delalladmin(){

		$ids = $_POST['arr'];
		$ids =  substr($ids, 0, -1);
		$strArr=explode(',',$ids);
        $username='';
		foreach ($strArr as $val){
            $sqls="SELECT * FROM `h_admin` WHERE id=".$val;
            $rows = $this->model->query($sqls);
            $username=$username.$rows[0]['username'].'、';
			$sql = "delete from h_admin where id=".$val;
			$row = $this->model->query($sql);			
		}
        $this->addStaffLog('删除用户','删除用户：'.$username,'user');
		$json ='{"msg":"'.$row.'"}';
		echo $json;
		exit;
	}


    public function permission(){

        $method = isset($_GET['m'])?$_GET['m']:'accountManagement';
        $this->assign("menuConfigArry",$_SESSION['manage_MenuConfigArry']['permission']);

        $childMenuArry=$this->getChindMenu('permission');
        if($childMenuArry){
            $this->assign("childMenuArry",$childMenuArry);
            $this->assign("method",$method);
        }
        call_user_func(array('systemMod',$method));


    }

    //角色权限管理
    public function propertyManagement(){
        require_once(CP_PATH.'ext/Page.class.php');
        //获取页数ID
        $page_id=intval($_GET['page']);
        $name = $_GET['name'];
        if($name){
            $condition = "rolename like '%".$name."%'";
            $con=" where rolename like '%".$name."%' ";
        }
        $url=__URL__.'/page?page='.$page_id;
        $page=new Page();
        //每页显示行数
        $listRows=10;//每页显示的信息条数
        //每页显示页数
        $pageBarNum=5;
        $cur_page=$page->getCurPage($url);

        $limit_start=($cur_page-1)*$listRows;
        $limit=$limit_start.','.$listRows;
        $limitStr='';
        if($limit!=''){
            $limitStr = " limit ".$limit;
        }

        $count=$this->model->table('h_permission')->where($condition)->count();

        $sql = " select * from h_permission ".$con.$limitStr;
        $list  =   $this->model->query($sql);

        $this->assign('count',$count);
        if($listRows>=$count){
            $this->assign('page','');
        }else{
            $this->assign('page',$page-> show($url,$count,$listRows,$pageBarNum,$mode=5) );
        }
        if(count($list)){
            $this->assign("permissionArr",$list);
        }

        $navigationMenu= $this->model->query("SELECT mf.`menuName` as pmenuName,mf.`menuLabel` as pmenuLabel,mc.* FROM `h_navigation_menu` mf left join `h_navigation_menu` mc on mf.id=mc.pid WHERE mf.pid=0 and mc.state=1 and mf.state=1 order by mf.morder,mc.morder");
        foreach($navigationMenu as $v){
            $pmenuName = $v['pmenuName'];
            $pmenuLabel = $v['pmenuLabel'];
            $menuName=$v['menuName'];
            $navigationMenuArry[$pmenuLabel]['pmenuName']=$pmenuName;
            $navigationMenuArry[$pmenuLabel]['pmenuLabel']=$pmenuLabel;
            if($menuName){
                $child=array(
                    'menuName'=>$v['menuName'],
                    'id'=>$v['id'],
                );
                $navigationMenuArry[$pmenuLabel]['child'][]=$child;
            }
        }
        $this->assign("navigationMenuArry",$navigationMenuArry);

        $this->display("system/propertyManagement");
    }

    //角色权限添加
    public function addpermission(){
        $json ="{";
        $rolename = $_POST['rolename'];
        $permission =  $_POST['arr'];
        $id  = $_POST['id'];

        if($id){
            $sql = "select * from h_permission where rolename='".$rolename."' and id !=".$id;
            $row  =   $this->model->query($sql);
            if($row){
                $json = '{"msg":"2"}';
                echo $json;
                exit;
            }
            $sql = "update h_permission set rolename='".$rolename."',permission='".$permission."' where id=".$id;
            $row = $this->model->query($sql);
        }else{
            $al = $this->search_already('rolename',$rolename,'h_permission');//判断用户是否存在
            if($al==1){
                $json = '{"msg":"2"}';
                echo $json;
                exit;
            }
            $sql = "insert into h_permission(rolename,permission,state,level) values".
                "('{$rolename}','{$permission}','0','0')";

            $row = $this->model->query($sql);
            if($row==1){
                $id =   $this->model->insert_id();
                $json .= "'id':'".$id."',";
            }
        }

        $json = '{"msg":"'.$row.'"}';
        echo $json;
        exit;
    }

    //权限删除
    public function delallpermission(){

        $ids = $_POST['arr'];
        $ids =  substr($ids, 0, -1);
        $strArr=explode(',',$ids);
        $username='';
        foreach ($strArr as $val){
            $sqls="SELECT * FROM `h_permission` WHERE id=".$val;
            $rows = $this->model->query($sqls);
            $username=$username.$rows[0]['rolename'].'、';
            $sql = "delete from h_permission where id=".$val;
            $row = $this->model->query($sql);
        }
        $this->addStaffLog('删除权限账号','删除权限账号：'.$username,'permission');
        $json ='{"msg":"'.$row.'"}';
        echo $json;
        exit;
    }

    //参数设置
	public function canshu(){
  		$row = $this->model->query("select * from h_canshu where state=1 order by id");
		if($row){
			$this->assign("row",$row);	
		}
        $this->assign("menuConfigArry",$_SESSION['manage_MenuConfigArry']['canshu']);
		$this->display("system/canshu");
	}
	
	//参数修改
	public function editcanshu(){
		$json ="{";
		$id  = $_POST['id'];
		$x = $_POST['x'];
		$y = $_POST['y'];
		$sql = "update h_canshu set x='".$x."',y='".$y."' where id=".$id;
		$row = $this->model->query($sql);
		$json .="'msg':'1'}";
		echo $json;
		exit;
	}


	
	//ip设置
	public function ipcfg(){
  		$row = $this->model->query("select * from h_ipcfg where  ip_status=1 ");
		if($row){
			$this->assign("row",$row);	
		}
        $this->assign("menuConfigArry",$_SESSION['manage_MenuConfigArry']['ipcfg']);
		$this->display("system/ipcfg");
	}

	//apk修改
	public function editip(){
		$json ="{";
		$id  = $_POST['id'];
		$ip_name= $_POST['ip_name'];
		$ip_address= $_POST['ip_address'];
		$netcard_num= $_POST['netcard_num'];
        $ip_status= $_POST['ip_status'];

		$sql = "update h_ipcfg set ip_name='".$ip_name."',ip_status='".$ip_status."',ip_address='".$ip_address."',netcard_num ='".$netcard_num."' where id=".$id;
		$row = $this->model->query($sql);
		$json .="'msg':'1'}";
		echo $json;
		exit;
	}
	
	public function addip(){

		$ip_name = $_POST['ip_name'];
		$ip_address= $_POST['ip_address'];
		$netcard_num= $_POST['netcard_num'];
        $ip_status= $_POST['ip_status'];

		$sql = "insert into h_ipcfg(ip_name,ip_address,netcard_num,ip_status) values"."('{$ip_name}','{$ip_address}','{$netcard_num}','{$ip_status}')";
		$row = $this->model->query($sql);

        $json ='{"msg":"1"}';
		echo $json;
		exit;
	}
public function delallip(){
		$ids = $_POST['arr'];
		$ids =  substr($ids, 0, -1);
		$strArr=explode(',',$ids);
		foreach ($strArr as $val){
			$sql = "delete from  h_ipcfg where id=".$val;
			$row = $this->model->query($sql);			
		}
		$json ='{"msg":"'.$row.'"}';
		echo $json;
		exit;
	}






}

?>


